#include <REGX52.H>
#include "Key.h"
#include "AT24C02.h"
#include "LCD1602.h"
#include "Delay.h"

//unsigned char Data;

unsigned char KeyNum;
unsigned int Num;

void main()
{
	LCD_Init();
	LCD_ShowNum(1,1,Num,5);
//	AT24C02_WriteByte(1,123);//在地址为1处，写66
	//连续读不用Delay,但是每次写都要Delay
//	Delay(5);//这里要考虑最大持续时间
	//写周期为5ms
//	Data=AT24C02_ReadByte(1);//读地址为1处的数据
//	LCD_ShowNum(1,1,Data,3);
	
	while(1)
	{
		KeyNum=Key();
		if(KeyNum==1)
		{
			Num++;
			LCD_ShowNum(1,1,Num,5);
		}
		if(KeyNum==2)
		{
			Num--;
			LCD_ShowNum(1,1,Num,5);
		}
		if(KeyNum==3)
		{
			//Num是16位数据，所以高8位和低8位要拆开
			AT24C02_WriteByte(0,Num%256);//低8位
			Delay(5);
			AT24C02_WriteByte(1,Num/256);//高8位
			Delay(5);
			LCD_ShowNum(1,1,Num,5);
			LCD_ShowString(2,1,"Write OK!");
			Delay(1000);
			LCD_ShowString(2,1,"		 ");
		}
		if(KeyNum==4)
		{
			Num=AT24C02_ReadByte(0);
			Num|=AT24C02_ReadByte(1)<<8;//高8位的处理,先赋值再左移
			LCD_ShowNum(1,1,Num,5);
			LCD_ShowString(2,1,"Read OK!");
			Delay(1000);
			LCD_ShowString(2,1,"	    ");
		}
	}
}//按下按键1,2：Num值加减，按键3,4分别为写和读